/* 文档载入完成后执行 */
$(() => {
    initCalendar();
});

const now = new Date();

/* 获取当前月有多少天 */
function getMonthDays() {
    return new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate();
}

/* 着重显示今天 */
function getToday() {
    // initCalendar();
    $(".date_item").siblings().removeClass("present");
    $($(".date_item")[now.getDate() - 1]).addClass("present");
}
/* 获取当前月份 */
function getMonth() {
    let months = ['', ''];
}

/* 初始化日历 */
function initCalendar() {
    let calendar = $("#dates");
    let daysNum = getMonthDays();
    calendar.html("");
    for (let i = 1; i <= daysNum; i++) {
        calendar.append("<div class='date_item'>" + i + "</div>")
    }

    // 添加事件
    let dateItems = $(".date_item");
    dateItems.on("click", function() {
        $(this).addClass("present").siblings().removeClass("present");
    });
    dayAlignment();
    getToday();
}

/* 日期对其 */
function dayAlignment() {
    let first = $(".date_item:first-child");
    let t = new Date();
    t.setDate(1);
    let theFirstDayOfMonth = t.getDay();
    switch(theFirstDayOfMonth) {
        case 1:
            first.css("margin-left", "0px");
            break;
        case 2:
            first.css("margin-left", "50px");
            break;
        case 3:
            first.css("margin-left", "100px");
            break;
        case 4:
            first.css("margin-left", "150px");
            break;
        case 5:
            first.css("margin-left", "200px");
            break;
        case 6:
            first.css("margin-left", "250px");
            break;
        case 7:
            first.css("margin-left", "300px");
            break;
    }
}